﻿using System.Threading.Tasks;
using Demo.Api.Data;
using Demo.Api.Models;
using Demo.Api.Services;
using Microsoft.AspNetCore.Mvc;

namespace Demo.Api.Controllers
{
    /// <summary>
    /// 反向时，清空的分流节点
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ReverseClearLineAndNodeController : ControllerBase
    {
        private readonly ReverseClearLineAndNodeRepository _repository;

        public ReverseClearLineAndNodeController(ReverseClearLineAndNodeRepository repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取一个流程图的所有的反向清空
        /// </summary>
        /// <param name="flowChartId">流程图Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Gets(long flowChartId)
        {
            var result = await _repository.GetsAsync(flowChartId);
            return Ok(result);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add(ReverseClearLineAndNodeDto model)
        {
            var temp = model.To();
            temp.Id = IdWorker.NextId();
            if (await _repository.Add(temp))
                return Ok(temp.Id);
            else
            {
                return BadRequest("添加失败");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Delete(long id)
        {
            var result = await _repository.Delete(id);
            return Ok(result);
        }
    }
}